<?php
namespace Admin\Controller;
use Think\Controller;
use Org\Util\String;
use Think\Page;
	class AdminController extends CommonController{
		public function index(){
			
			$admin = D('Admin'); // 实例化User对象
			$count      = $admin->count();// 查询满足要求的总记录数
			$Page       = new Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $admin->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign('data',$list);// 赋值数据集
			$this->assign('page',$show);// 赋值分页输出
			
			$this->display('Admin/index');
		}
		
		
		public function del(){
			$m=M('Admin');
			$count = $m ->count();
			if($count<=1){
				$this->error('唯一管理员不能删除');
			}
			
			$id=$_GET['id'];
			$num=$m->delete($id);
			if($num>0){
				$this->success('删除用户成功');
			}else{
				$this->error('数据删除失败');
			}
		}
		/*
		 *	显示修改页面
		 * */
		public function modify(){
			$id=$_GET['id'];
			$m=M('Admin');
			$arr=$m->find($id);
			$this->assign('data',$arr);
			
			$this->display('Admin/edit');
		}
		
		public function update(){
			$m=D('Admin');
			
			$data = array('username'=>$_POST['username']);
			$count=$m-> where('id='.$_POST['id'])->setField($data);
			
			if($count>0){
				$this->success('数据修改成功','index');
			}else{
				$this->error('数据修改失败');
			}
		}
		
		public function changepwd(){
			$admin=D('Admin');
			$salt = String::randString(10);
			$data['salt'] = $salt;
			$data['password'] = md5($_POST['password'].$salt);
			$count = $admin-> where('id='.$_POST['id'])->setField($data);
			if($count>0){
				$this->success('数据修改成功','index');
			}else{
				$this->error('数据修改失败');
			}
		}
		
		
		public function checkusername(){
			$admin=M('Admin');
			$data = array('username'=>$_POST['username']);
			$count = $admin-> where($data)->find();
			if($count == null){
				$this->show("error");
			}else{
				$this->show("ok");
			}
		}
		
	/*
		 * 添加页面
		 * */
		public function add(){
			$this->display('Admin/add');
		}
		
		public function create(){
			$m=D('Admin');
			$salt = String::randString(10);
			$data['salt'] = $salt;
			$data['password'] = md5($_POST['password'].$salt);
			$data['created_at'] = date("Y-m-d H:i:s");
			$data['username'] = $_POST['username'];
			if(!$m->create($data)){
				$this->error($m->getError());
			}
			$idNum=$m->add();
			if($idNum>0){
				$this->success('添加用户成功','index');
			}else{
				$this->error('数据添加失败');
			}
		}
	}
?>
